﻿using DevExpress.XtraEditors.Controls;
using Marmat.Forms.Design;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace Marmat.Forms.Design
{
    public partial class CreatecontrolsForm : Form
    {
        private System.Windows.Forms.Form _form;
        private static Type[] controlCollection;
        private IBindingSource _bindingSource;

        public CreatecontrolsForm(System.Windows.Forms.Form form, IBindingSource bindingSource)
        {
            InitializeComponent();
            this._form = form;
            this._bindingSource = bindingSource;
        }

        private void CreatecontrolsForm_Load(object sender, EventArgs e)
        {
            foreach (IComponent component in this._form.Container.Components)
            {
                if (component is System.Windows.Forms.SplitContainer splitContainer)
                {
                    this.comboBox1.Items.Add(splitContainer.Panel1);
                    this.comboBox1.Items.Add(splitContainer.Panel2);
                }
                else if (component is IControl control)
                {
                    if (component is IContainerControl)
                        this.comboBox1.Items.Add(component);
                }
                else if (component is System.Windows.Forms.IContainerControl || component is System.Windows.Forms.Panel || component is System.Windows.Forms.GroupBox)
                    this.comboBox1.Items.Add(component);
            }
            this.comboBox1.SelectedValue = this._form;
            this.BindingSource1.DataSource = this._bindingSource.GetDataSource();
            System.ComponentModel.PropertyDescriptorCollection collection = this.BindingSource1.GetItemProperties(null);
            foreach (var column in this._bindingSource.GetColumnCollection())
            {
                Marmat.Forms.Views.DataGrid.Row row = this.BDataGrid1.AddNew();
                row["Check"] = false;
                row["ColName"] = column.Name;
                row["ColType"] = column.DataType;
                row["Caption"] = (column.Name != column.Caption) ? column.Caption : null;

            }

            this.BDataGrid1.AcceptChanges();
            this.BDataGrid1.Position = 0;
            Type type = typeof(IBindingField);

            DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox repositoryItem = new DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox();
            repositoryItem.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            repositoryItem.DropDownItemHeight = 24;
            repositoryItem.ItemAutoHeight = false;

            if (controlCollection is null && Services.SmartMatchControlService != null)
            {
                controlCollection = Marmat.Forms.AsyncHelper.Run(() => Services.SmartMatchControlService.GetCollection());
            }
            if (controlCollection != null)
            {
                int imageIndex = 0;
                DevExpress.Utils.ImageCollection imageCollection = new DevExpress.Utils.ImageCollection();
                foreach (var item in controlCollection)
                {
                    ToolboxItemAttribute toolboxItem = item.GetCustomAttributes<ToolboxItemAttribute>().FirstOrDefault();
                    if (toolboxItem is MarmatToolboxItemAttribute marmatToolboxItem && marmatToolboxItem.DefaultType == false)
                        continue;

                    ImageComboBoxItem boxItem = new ImageComboBoxItem();
                    boxItem.Value = item;
                    boxItem.Description = item.Name;
                    ToolboxBitmapAttribute toolboxBitmap = item.GetCustomAttributes<ToolboxBitmapAttribute>().FirstOrDefault();
                    if (toolboxBitmap != null)
                    {
                        Image image = toolboxBitmap.GetImage(item);
                        imageCollection.AddImage(image);
                        boxItem.ImageIndex = imageIndex;
                        imageIndex++;
                    }
                    repositoryItem.Items.Add(boxItem);
                }
                repositoryItem.SmallImages = imageCollection;
            }

            this.BDataGrid1.Columns["Type"].GridColumn.ColumnEdit = repositoryItem;
        }

        private void BButton3_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.BDataGrid1.Rows)
            {
                row["Check"] = true;
            }
        }

        private void BButton4_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.BDataGrid1.Rows)
            {
                row["Check"] = !Convert.ToBoolean(row["Check"]);
            }
        }

        private void BButton5_Click(object sender, EventArgs e)
        {
            foreach (Marmat.Forms.Views.DataGrid.Row row in this.BDataGrid1.Rows)
            {
                if (true.Equals(row["Check"]))
                {
                    SmartMatchControlInfo smart = new SmartMatchControlInfo();
                    smart.PropertyName = row["ColName"] as string;
                    smart.PropertyType = row["ColType"] as Type;
                    smart.Caption = row["Caption"] as string;
                    Type type = Services.SmartMatchControlService.Match(smart);
                    row["Type"] = type;
                }
                else
                {
                    row["Type"] = null;
                }
            }
        }

        private void BComboBox1_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            if (e.Value is System.Windows.Forms.SplitterPanel panel)
                e.DisplayText = panel.Parent.Name + "." + panel.Name;
            else if (e.Value is System.ComponentModel.Component component)
            {
                if (e.Value is System.Windows.Forms.Control control && !string.IsNullOrEmpty(control.Text))
                    e.DisplayText = $"{component.Site.Name}[{control.Text}]";
                else
                    e.DisplayText = component.Site.Name;
            }
            else if (e.Value is System.Windows.Forms.Control control)
                e.DisplayText = $"{control.Name}[{control.Text}]";
        }
    }
}
